package com.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mainService.mapper.MainStoreMapper;
import com.mainService.mapper.PowerMapper;
import com.mainService.mapper.UserMapper;
import com.mainService.service.UserService;
import com.projectCommon.bean.MainStore;
import com.projectCommon.bean.Power;
import com.projectCommon.bean.User;
import com.projectCommon.bean.dto.LoginDto;
import com.projectCommon.bean.dto.UserDto;
import com.projectCommon.bean.result.Result;
import com.projectCommon.util.commonUtil.PermissionVerification;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MainStoreMapper mainStoreMapper;
    @Autowired
    private PowerMapper powerMapper;

    public List<UserDto> selectList() {
        List<User> list = this.list();
        List<UserDto> userDtos = new ArrayList<>();
        for (User user : list) {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(user, userDto);

            MainStore mainStore = mainStoreMapper.selectById(user.getAffiliatedHeadId());
            userDto.setHeadStoreName(mainStore.getName());

            LambdaQueryWrapper<Power> powerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            powerLambdaQueryWrapper.eq(Power::getUserId, user.getId());
            powerLambdaQueryWrapper.select(Power::getPowerDetail);
            Power power = powerMapper.selectOne(powerLambdaQueryWrapper);
//            TODO：需要修改。数据库缺信息。！！
            power.setPowerDetail("B3");
            String powerDescription = PermissionVerification.getPowerName(power.getPowerDetail());
            userDto.setPowerName(powerDescription);
            userDtos.add(userDto);
        }
        return userDtos;
    }

    /**
     * 根据传入信息判断信息是否有效
     *
     * @param loginDto 账号 密码 验证码
     * @return 返回值
     * TODO: 优化：空值校验，查询的时候不要查询全部字段
     */

    @Override
    public Result judge(LoginDto loginDto) {

//        一：判断账号是否可以登录
        String loginId = loginDto.getLoginId();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(loginId != null, User::getLoginId, loginId);
        User user = userMapper.selectOne(userLambdaQueryWrapper);
//      1. 判断账号是否存在
        if (user == null) {
            Result.fail("账号不存在");
        }
//      2. 判断密码是否正确
        String encipher = loginDto.getPassword() + user.getSalt();
        String md5 = DigestUtils.md5DigestAsHex(encipher.getBytes());
        String loginPassword = user.getLoginPassword();
        if (!md5.equals(loginPassword)) {
            return Result.fail("密码错误");
        } else {
//        2.1. 验证码是否正确
            String checkCode = loginDto.getCheckCode();
            if (!checkCode.equals("123456")) {
                return Result.fail("验证码错误");
            }
        }

//      3. 判断是否允许登录
        if (!user.getLoginDetermine().equals('1')) {
            return Result.fail("此账户不允许登录");
        }
        return Result.success("验证成功，点击跳转");
    }
}
